package com.atguigu.lease.task;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class CheckAndUpdateAgreementStatusTask {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    //设置定时任务
    @Scheduled(cron = "0 46 14 * * ?")
    public void taskTest(){
        //1.创建LambdaUodateWrapper对象
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //2.封装查询条件
        //2.1 租约结束日期  小于等于当前日期
        lambdaUpdateWrapper.le(LeaseAgreement::getLeaseEndDate,new Date());
        //租约状态是2,5,7
        lambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        //封装修改的列
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        //不加where条件，某个租约  定时任务检测的是所有的租约，达到租约时间了，就更改租约状态
        leaseAgreementService.update(lambdaUpdateWrapper);
    }
}
